package com.app.bimo.module_accounts.mvp.persenter;


import com.alibaba.android.arouter.launcher.ARouter;
import com.app.bimo.commonbusiness.mvp.BasePresenter;
import com.app.bimo.commonbusiness.network.RetrofitServiceManager;
import com.app.bimo.commonbusiness.network.RxObservableUtil;
import com.app.bimo.commonbusiness.util.Constant;
import com.app.bimo.commonbusiness.util.DataUtil;
import com.app.bimo.commonbusiness.util.LoginService;
import com.app.bimo.commonbusiness.util.RouterHub;
import com.app.bimo.module_accounts.mvp.constract.LoginContract;
import com.app.bimo.module_accounts.mvp.model.entiy.OtherResult;

import androidx.collection.ArrayMap;

public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
    public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);

    }


    public void requestFromModel(int type, String openid, String unionid, String nickname, String avatar, int sex, String fromChannel) {
        ArrayMap map = new ArrayMap();
        map.put("type", type);
        map.put("openid", openid);
        map.put("unionid", unionid);
        map.put("nickname", nickname);
        map.put("avatar", avatar);
        map.put("sex", sex);
        if (!DataUtil.isEmpty(fromChannel)) {
            map.put("fromChannel", fromChannel);
        }
        RxObservableUtil.subscribe(mModel.login(map), bindCustomLoadingLifecycle(true),
                bookStoreDataList -> {
                    // 处理数据 直接获取到List<JavaBean> carBeans
                    mRootView.loginDataNotify(type, (OtherResult) bookStoreDataList);
                });
    }


    public void getCodeFromModel(String mobile, int type) {

        RxObservableUtil.subscribe(mModel.getCode(mobile, type), bindCustomLoadingLifecycle(true), bookStoreDataList -> {
            // 处理数据 直接获取到List<JavaBean> carBeans
            mRootView.getCodeDataNotify(bookStoreDataList);
        });
    }

    public void registerFromModel(ArrayMap<String, String> map) {
        RxObservableUtil.subscribe(mModel.phoneRegister(map), bindCustomLoadingLifecycle(true), bookStoreDataList -> {
            // 处理数据 直接获取到List<JavaBean> carBeans
//            getUserInfoFromModel();
            RetrofitServiceManager.getInstance().addAllHead(Constant.HttpToken, bookStoreDataList.getToken());
            ((LoginService) ARouter.getInstance().build(RouterHub.ACCOUNT_SERVICE).navigation()).getUserInfo();
            mRootView.registerDataNotify(bookStoreDataList, map.get("mobile"));
        });
    }

//    /**
//     * 获取用户资料保存到db
//     */
//    public void getUserInfoFromModel() {
//        RxObservableUtil.subscribe(mModel.getUserInfo(), bindCustomLifecycle(), bookStoreDataList -> {
//            // 处理数据 直接获取到List<JavaBean> carBeans
//            if (UserHelper.getsInstance().findUser() != null) {
//                UserHelper.getsInstance().removeUser();
//            }
//            UserHelper.getsInstance().saveUser(bookStoreDataList);
//            mRootView.userInfoNotify(bookStoreDataList);
//        });
//    }

    /**
     * 收不到验证码弹窗内容和客服微信号
     */
    public void getCodeHelp() {
        RxObservableUtil.subscribe(mModel.getCodeHelp(), bindCustomLifecycle(), bookStoreDataList -> {
            mRootView.getCodeHelp(bookStoreDataList);
        });
    }
}
